import { post } from "@/common/axios.js";
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    user: {},
    statusRandom: 0,
    loginoutRandom: 0,
    chatting: [],
    toBottomRandom: 0,
    history: [],
    selectList: [],
    currentChat: {},
    weChatList: [], // 微信列表
    currentWeChat: {}, // 当前选中微信
    userPackageInfo: {}, // 用户账号信息
    scanId: "", // 扫码登录时的微信ID
    port: "",
    taskName: "",
    taskWeChatId: "",
    taskWechatName: "",
    hasUnreadMsg: false,
  },
  mutations: {
    userState(state, provider) {
      state.user = provider;
    },
    weChatListState(state, provider) {
      state.weChatList = provider;
    },
    userPackageInfoState(state, provider) {
      state.userPackageInfo = provider;
    },
    currentWeChatState(state, provider) {
      state.currentWeChat = provider;
    },
    chattingState(state, provider) {
      state.chatting = provider;
    },
    hasUnreadMsgState(state, provider) {
      state.hasUnreadMsg = provider;
    },
  },
  actions: {
    getUser(context) {
      // 获取用户信息
      post("/front/user/userInfo")
        .then((res) => {
          if (res.data.code === 0) {
            console.log("获取用户基本信息", res.data.obj);
            window.localStorage.setItem("user", res.data.obj);
            context.commit("userState", res.data.obj);
            context.dispatch("getWeChatList");
            context, this.dispatch("getUserPackageInfo");
          }
        })
        .catch((err) => {
          console.log("获取用户信息失败", err);
        });
    },

    getWeChatList(context) {
      post("/front/userWechat/wechatList", {
        pageNum: 1,
        pageSize: 1000,
      }).then((res) => {
        if (res.data.code === 0) {
          let list = res.data.obj.list.filter((o) => {
            return o.wechatName;
          });
          if (list.length === 0) return;
          console.log("获取用户已经登录过的微信列表", list);
          context.commit("weChatListState", list);
          let userWeChatId = window.sessionStorage.getItem("currentWeChatId");
          let userWechatName = window.sessionStorage.getItem(
            "currentUserWechatName"
          );
          let onLineStatus = window.sessionStorage.getItem(
            "currentOnLineStatus"
          );
          if (userWeChatId && userWechatName && onLineStatus) {
            context.commit("currentWeChatState", {
              currentWeChatId: userWeChatId,
              currentUserWechatName: userWechatName,
              currentOnLineStatus: onLineStatus,
            });
            // context.dispatch("getChatting", userWeChatId)
          } else {
            context.commit("currentWeChatState", {
              currentWeChatId: list[0].id,
              currentUserWechatName: list[0].wechatName,
              currentOnLineStatus: list[0].status,
            });
            // context.dispatch("getChatting", list[0].id)
          }
        }
      });
    },

    getUserPackageInfo(context) {
      post("/front/child/userPackageInfo").then((res) => {
        if (res.data.code === 0) {
          console.log("获取用户账号信息", res.data.obj);
          context.commit("userPackageInfoState", res.data.obj);
        }
      });
    },

    getChatting(context, obj) {
      post("/front/wechat/friend/selectSessionList", {
        userWechatPrimaryId: obj.userWechatPrimaryId,
        userWechatName: obj.userWechatName,
        automaticResponse: obj.automaticResponse,
        categoryId: obj.categoryId,
        tag: obj.tag,
        content: obj.content,
      }).then((res) => {
        if (res.data.code === 0) {
          console.log("获取会话列表", res.data.obj);
          context.commit("chattingState", res.data.obj);
          res.data.obj.forEach((element) => {
            if (element.unreadNum && element.unreadNum > 0) {
              context.commit("hasUnreadMsgState", true);
              return;
            }
          });
        }
      });
    },
  },
});
